package com.nuaa.service.ret.impl;

import java.io.File;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.nuaa.dao.ret.inter.ReturnDaoInter;
import com.nuaa.dao.ret.inter.RetSupDaoInter;
import com.nuaa.domain.ret.RetSupEntity;
import com.nuaa.domain.ret.ReturnEntity;
import com.nuaa.formmodel.RetHandForm;
import com.nuaa.service.ret.inter.ReturnServiceInter;
import com.nuaa.utils.AjaxMsg;
import com.nuaa.utils.BusinessException;
import com.nuaa.utils.DataGrid;
import com.nuaa.utils.PageInfo;
@Service("returnService")
public class ReturnServiceImpl implements ReturnServiceInter{
	@Resource(name="returnDao")
	private ReturnDaoInter returnDao;
	@Resource(name="retSupDao")
	private RetSupDaoInter retSupDao;
	/**获取所有订单*/
	@Override
	public DataGrid<Map<String, Object>> getReturnOrders(PageInfo pager,
			String customer_no,String orderNo,String tmFrom,String tmTo) {
		StringBuilder sql=new StringBuilder();
		sql.append("select * from return_order where 1=1 ");
		if(customer_no!=null &&!"".equals(customer_no)){
			sql.append(" and customer_no ='");
			sql.append(customer_no);
			sql.append("'");
		}
		if(orderNo!=null && !"".equals(orderNo)){
			sql.append(" and order_no ='");
			sql.append(orderNo);
			sql.append("'");
		}
		if(tmFrom!=null && !"".equals(tmFrom) && tmTo!=null && !"".equals(tmTo)){
			sql.append(" and create_tm >='");
			sql.append(tmFrom);
			sql.append("' and create_tm <= '");
			sql.append(tmTo);
			sql.append("'");
		}
		//System.out.println(sql.toString());
		try{
			long total=returnDao .count(sql.toString());
			int []fm=pager.getFirstindexAndMaxindex();
			List<Map<String,Object>> list=returnDao.searchForMap(sql.toString(),fm[0],fm[1]," create_tm desc ");
			return new DataGrid<Map<String, Object>>(total, list);
		}catch(Exception e){
			throw new BusinessException("获取订单信息出错！",e);
		}
	}
	
	@Override
	public DataGrid<Map<String, Object>> getWaitReturnOrders(PageInfo pager,
			String customer_no,String orderNo,String tmFrom,String tmTo) {
		StringBuilder sql=new StringBuilder();
		sql.append("select * from return_order where state=0 ");
		if(customer_no!=null &&!"".equals(customer_no)){
			sql.append(" and customer_no ='");
			sql.append(customer_no);
			sql.append("'");
		}
		if(orderNo!=null && !"".equals(orderNo)){
			sql.append(" and order_no ='");
			sql.append(orderNo);
			sql.append("'");
		}
		if(tmFrom!=null && !"".equals(tmFrom) && tmTo!=null && !"".equals(tmTo)){
			sql.append(" and create_tm >='");
			sql.append(tmFrom);
			sql.append("' and create_tm <= '");
			sql.append(tmTo);
			sql.append("'");
		}
		//System.out.println(sql.toString());
		try{
			long total=returnDao .count(sql.toString());
			int []fm=pager.getFirstindexAndMaxindex();
			List<Map<String,Object>> list=returnDao.searchForMap(sql.toString(),fm[0],fm[1]," create_tm desc ");
			return new DataGrid<Map<String, Object>>(total, list);
		}catch(Exception e){
			throw new BusinessException("获取订单信息出错！",e);
		}
	}
	
	@Override
	public DataGrid<Map<String, Object>> getProceReturnOrders(PageInfo pager, String customer_no, String orderNo,
			String tmFrom, String tmTo) {
		StringBuilder sql=new StringBuilder();
		sql.append("select * from return_order where state>0 and state<6 ");
		if(customer_no!=null &&!"".equals(customer_no)){
			sql.append(" and customer_no ='");
			sql.append(customer_no);
			sql.append("'");
		}
		if(orderNo!=null && !"".equals(orderNo)){
			sql.append(" and order_no ='");
			sql.append(orderNo);
			sql.append("'");
		}
		if(tmFrom!=null && !"".equals(tmFrom) && tmTo!=null && !"".equals(tmTo)){
			sql.append(" and create_tm >='");
			sql.append(tmFrom);
			sql.append("' and create_tm <= '");
			sql.append(tmTo);
			sql.append("'");
		}
		//System.out.println(sql.toString());
		try{
			long total=returnDao .count(sql.toString());
			int []fm=pager.getFirstindexAndMaxindex();
			List<Map<String,Object>> list=returnDao.searchForMap(sql.toString(),fm[0],fm[1]," create_tm desc ");
			return new DataGrid<Map<String, Object>>(total, list);
		}catch(Exception e){
			throw new BusinessException("获取订单信息出错！",e);
		}
	}

	@Override
	public DataGrid<Map<String, Object>> getFinishReturnOrders(PageInfo pager,
			String customer_no,String orderNo,String tmFrom,String tmTo) {
		StringBuilder sql=new StringBuilder();
		sql.append("select * from return_order where state=6 ");
		if(customer_no!=null &&!"".equals(customer_no)){
			sql.append(" and customer_no ='");
			sql.append(customer_no);
			sql.append("'");
		}
		if(orderNo!=null && !"".equals(orderNo)){
			sql.append(" and order_no ='");
			sql.append(orderNo);
			sql.append("'");
		}
		if(tmFrom!=null && !"".equals(tmFrom) && tmTo!=null && !"".equals(tmTo)){
			sql.append(" and create_tm >='");
			sql.append(tmFrom);
			sql.append("' and create_tm <= '");
			sql.append(tmTo);
			sql.append("'");
		}
		//System.out.println(sql.toString());
		try{
			long total=returnDao .count(sql.toString());
			int []fm=pager.getFirstindexAndMaxindex();
			List<Map<String,Object>> list=returnDao.searchForMap(sql.toString(),fm[0],fm[1]," create_tm desc ");
			return new DataGrid<Map<String, Object>>(total, list);
		}catch(Exception e){
			throw new BusinessException("获取订单信息出错！",e);
		}
	}
	/**保存一条订单信息*/
	@Override
	public AjaxMsg saveOrder(ReturnEntity entity) {
		entity.setBusiness_type("FBA退货业务");
		return returnDao.saveOrder(entity);
	}
	@Override
	public ReturnEntity getById(int id) {
		return returnDao.find(ReturnEntity.class, id);
	}
	
	@Transactional
	@Override
	public void update(ReturnEntity returnor) {
		returnDao.update(returnor);
		
	}
	@Override
	public DataGrid<Map<String, Object>> getReturnUnckeck(PageInfo pager) {
		StringBuilder sql = new StringBuilder();
		sql.append("select * from return_order where 1=1 and state = 0 ");
		try {
			long total = returnDao.count(sql.toString());
			int[] fm = pager.getFirstindexAndMaxindex();
			List<Map<String, Object>> list = returnDao.searchForMap(sql.toString(), fm[0], fm[1], " create_tm desc ");
			return new DataGrid<Map<String, Object>>(total, list);
		} catch (Exception e) {
			throw new BusinessException("获取订单信息出错！", e);
		}
	}
	@Transactional
	@SuppressWarnings("deprecation")
	@Override
	public void checkRetState() {
		//选取包月服务的订单
		String sql = "select * from return_order where service_type = 0 ";
		List<ReturnEntity> list = returnDao.search(ReturnEntity.class, sql);
		Date now = new Date();
		for(ReturnEntity entity : list){
			Date lastDate = entity.getCreate_tm();
			//年份或者月份不一致来判断其是否
			if((lastDate.getYear()!=now.getYear()) || (lastDate.getMonth() != now.getMonth())){
				entity.setState(0);
				entity.setCreate_tm(now);
				returnDao.save(entity);
			}
		}
	}
	/////////////////////////////////////////////////////////////////
	@Transactional
	@Override
	public AjaxMsg subSup(RetSupEntity entity) {
		AjaxMsg msg = new AjaxMsg();
		try{
			//首先判断是否提交过该订单的补充文档
			StringBuilder sql=new StringBuilder();
			sql.append("select * from ret_sup where order_no = '");
			sql.append(entity.getOrder_no());
			sql.append("'");
			List<RetSupEntity> list = retSupDao.search(RetSupEntity.class, sql.toString());
			if(list==null || list.size()==0){
				retSupDao.save(entity);
			}else{
				/***
				 * 此处既然是覆盖就该删掉原有的文档，留做以后修正吧
				 */
				RetSupEntity temp = list.get(0);
				if(!entity.getRet_remove_logis_name().equals("")){
					this.delFileByName(temp.getRet_remove_logis_name());
					temp.setRet_remove_logis_name(entity.getRet_remove_logis_name());
				}
				if(!entity.getRet_deliver_number_name().equals("")){
					this.delFileByName(temp.getRet_deliver_number_name());
					temp.setRet_deliver_number_name(entity.getRet_deliver_number_name());
				}
				if(!entity.getRet_check_method_name().equals("")){
					this.delFileByName(temp.getRet_check_method_name());
					temp.setRet_check_method_name(entity.getRet_check_method_name());
				}
				if(!entity.getRet_address().equals("")){
					this.delFileByName(temp.getRet_address());
					temp.setRet_address(entity.getRet_address());
				}
				if(!entity.getRet_handle_name().equals("")){
					this.delFileByName(temp.getRet_handle_name());
					temp.setRet_handle_name(entity.getRet_handle_name());
				}
					
				if(!entity.getOther_file().equals("")){
					this.delFileByName(temp.getOther_file());
					temp.setOther_file(entity.getOther_file());
				}
				
				if(!entity.getOther_file2().equals("")){
					this.delFileByName(temp.getOther_file2());
					temp.setOther_file2(entity.getOther_file2());
				}
				if(!entity.getOther_file3().equals("")){
					this.delFileByName(temp.getOther_file3());
					temp.setOther_file3(entity.getOther_file3());
				}
				if(!entity.getOther_file4().equals("")){
					this.delFileByName(temp.getOther_file4());
					temp.setOther_file4(entity.getOther_file4());
				}
				retSupDao.update(temp);
			}
			msg.setSuccess(true);
			msg.setMsg("提交成功！");
		}catch(Exception e){
			e.printStackTrace();
			msg.setSuccess(false);
			msg.setMsg("提交失败！");
		}
		return msg;
	}
	@Override
	public ReturnEntity getByOrderNo(String order_no) {
		StringBuilder sql=new StringBuilder();
		sql.append("select * from return_order where order_no = '");
		sql.append(order_no);
		sql.append("'");
		List<ReturnEntity> list = returnDao.search(ReturnEntity.class, sql.toString());
		return list.get(0);
	}
	@Override
	public RetSupEntity getSupByOrderNo(String order_no) {
		StringBuilder sql=new StringBuilder();
		sql.append("select * from ret_sup where order_no = '");
		sql.append(order_no);
		sql.append("'");
		List<RetSupEntity> list = retSupDao.search(RetSupEntity.class, sql.toString());
		if(list.size()==0)
			return null;
		return list.get(0);
	}
	@Override
	public void delFileByName(String fileName) {
		String dir = System.getProperty("webApp.path")+"file";
		if(!"".equals(fileName)){
			File file = new File(dir+"\\"+fileName);
			if(file.exists()){
				file.delete();
			}
		}
	}
	@Transactional
	@Override
	public AjaxMsg saveModOrder(ReturnEntity entity) {
		AjaxMsg msg = new AjaxMsg();
		ReturnEntity retEntity = returnDao.find(ReturnEntity.class, entity.getId());
		retEntity.setPro_length(entity.getPro_length());
		retEntity.setPro_width(entity.getPro_width());
		retEntity.setPro_height(entity.getPro_height());
		retEntity.setMon_total(entity.getMon_total());
		retEntity.setType_numbers(entity.getType_numbers());
		retEntity.setIf_check(entity.getIf_check());
		retEntity.setBreak_check(entity.getBreak_check());
		retEntity.setBad_handle(entity.getBad_handle());
		retEntity.setEva_method(entity.getEva_method());
		try{
			returnDao.save(retEntity);
			msg.setSuccess(true);
			msg.setMsg("修改成功！");
		}catch (Exception e) {
			msg.setSuccess(false);
			msg.setMsg("修改失败！");
		}
		return msg;
	}
	@Transactional
	@Override
	public AjaxMsg saveHand(String id, RetHandForm form) {
		AjaxMsg msg = new AjaxMsg();
		try{
				ReturnEntity entity = returnDao.find(ReturnEntity.class,Integer.parseInt(id));
				entity.setState(form.getState());
				entity.setLaborFee(form.getLaborFee());
				entity.setPostage(form.getPostage());
				
				if(form.getCheck_result_tab_name()!=null&&!form.getCheck_result_tab_name().equals("")){
					this.delFileByName(entity.getCheck_result_tab_name());
					entity.setCheck_result_tab_name(form.getCheck_result_tab_name());
				}
				if(form.getRet_fee_tab_name()!=null&&!form.getRet_fee_tab_name().equals("")){
					this.delFileByName(entity.getRet_fee_tab_name());
					entity.setRet_fee_tab_name(form.getRet_fee_tab_name());
				}
				if(form.getRet_model_name()!=null&&!form.getRet_model_name().equals("")){
					this.delFileByName(entity.getRet_model_name());
					entity.setRet_model_name(form.getRet_model_name());
				}
				if(form.getRet_scrap_tab_name()!=null&&!form.getRet_scrap_tab_name().equals("")){
					this.delFileByName(entity.getRet_scrap_tab_name());
					entity.setRet_scrap_tab_name(form.getRet_scrap_tab_name());
				}
				if(form.getArr_tab_name()!=null&&!form.getArr_tab_name().equals("")){
					this.delFileByName(entity.getArr_tab_name());
					entity.setArr_tab_name(form.getArr_tab_name());
				}
				
				if(form.getLogis_tab_name()!=null&&!form.getLogis_tab_name().equals("")){
					this.delFileByName(entity.getLogis_tab_name());
					entity.setLogis_tab_name(form.getLogis_tab_name());
				}
				
				if(form.getRec_tab_name()!=null&&!form.getRec_tab_name().equals("")){
					this.delFileByName(entity.getRec_tab_name());
					entity.setRec_tab_name(form.getRec_tab_name());
				}
				if(form.getOther_file1()!=null&&!form.getOther_file1().equals("")){
					this.delFileByName(entity.getOther_file1());
					entity.setOther_file1(form.getOther_file1());
				}
				if(form.getOther_file2()!=null&&!form.getOther_file2().equals("")){
					this.delFileByName(entity.getOther_file2());
					entity.setOther_file2(form.getOther_file2());
				}
				if(form.getOther_file3()!=null&&!form.getOther_file3().equals("")){
					this.delFileByName(entity.getOther_file3());
					entity.setOther_file3(form.getOther_file3());
				}
				if(form.getOther_file4()!=null&&!form.getOther_file4().equals("")){
					this.delFileByName(entity.getOther_file4());
					entity.setOther_file4(form.getOther_file4());
				}
				returnDao.update(entity);
			msg.setSuccess(true);
			msg.setMsg("操作成功！");
		}catch (Exception e) {
			e.printStackTrace();
			msg.setSuccess(false);
			msg.setMsg("操作失败！");
		}
		return msg;
	}
}
